<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.png">
  <link rel="mask-icon" href="/images/avatar.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"notes.maxwi.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Shell脚本简单入门，如果你分不清shell是什么，此文一定不适合你。毕竟只花了十多分钟看的教程，不会涉及细节，细节将在以后的使用中逐渐以示例形式给出。">
<meta property="og:type" content="article">
<meta property="og:title" content="Shell脚本简单入门">
<meta property="og:url" content="http://notes.maxwi.com/2017/04/23/shell-script/index.html">
<meta property="og:site_name" content="blueyi&#39;s notes">
<meta property="og:description" content="Shell脚本简单入门，如果你分不清shell是什么，此文一定不适合你。毕竟只花了十多分钟看的教程，不会涉及细节，细节将在以后的使用中逐渐以示例形式给出。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2017-04-23T11:21:00.000Z">
<meta property="article:modified_time" content="2017-04-23T11:21:00.000Z">
<meta property="article:author" content="blueyi">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="shell">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://notes.maxwi.com/2017/04/23/shell-script/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>Shell脚本简单入门 | blueyi's notes</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="blueyi's notes" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="blueyi's notes" type="application/rss+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">blueyi's notes</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Follow Excellence,Success will chase you!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://notes.maxwi.com/2017/04/23/shell-script/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/default_avatar.jpg">
      <meta itemprop="name" content="blueyi">
      <meta itemprop="description" content="心怀善意，虛怀若谷！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="blueyi's notes">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Shell脚本简单入门
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2017-04-23 19:21:00" itemprop="dateCreated datePublished" datetime="2017-04-23T19:21:00+08:00">2017-04-23</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="Symbols count in article">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">Symbols count in article: </span>
              <span>7.1k</span>
            </span>
            <span class="post-meta-item" title="Reading time">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">Reading time &asymp;</span>
              <span>6 mins.</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>Shell脚本简单入门，如果你分不清shell是什么，此文一定不适合你。毕竟只花了十多分钟看的教程，不会涉及细节，细节将在以后的使用中逐渐以示例形式给出。</p>
<span id="more"></span>

<p>PS:借来的东西，果然迟早是要还的。刚在linux下学习时，为了批量处理，当时会C语言，就用C语言编个程序实现，心想虽然不会shell，不是也很方便嘛。然后会了C++，发现比C用起来方便多了。再然后越来越的批处理工作需要程序，C++有点太大杀气了，Python这么火，而且也很强大，学了肯定不白学，遂各种自动化的工作就交给了Python。然后最近发现Python这货也有点太大杀所了，我只是想写个简单的自动化指令而已。所以现在来还当年欠下的shell。假如你也如此，还是花上几十分钟，掌握一把小刀，何乐而不为呢。</p>
<h2 id="关于"><a href="#关于" class="headerlink" title="关于"></a>关于</h2><p>记得脚本最前面添加shell解释器，通常为sh、bash，其他的不常见，依然先给个HelloWorld</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line">echo &quot;HelloWorld&quot; #我是注释</span><br></pre></td></tr></table></figure>
<p>然后给了执行权限即可直接执行，或者手动通过<code>sh script.sh</code>来执行，shell脚本后缀通常为<code>.sh</code>。当然Linux下面文件只要有了可执行权限（chmod +x file），什么后缀都没有影响。<br>我们平时使用的terminal其实就是个shell解释器的交互窗口，所以学习shell编程直接开个terminal就可以开编了。shell中可以直接执行任何linux/unix命令（其实这些命令就是shell内部的）正是它方便之处</p>
<h2 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h2><p>shell变量不分类型，默认都是字符串。单引号的字符串类似raw字符串，不能解析转义字符及变量。不被引号引住的字符串中不能用空格。<br>变量定义形如：变量名=值，等号中间不能有空格。变量名要求即编程语言的常规要求<br>变量引用则直接在变量名前加<code>$</code>符号，为了限定shell解释器解释变量名的边界可以用大括号包围变量名：${变量名}<br>ex：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var=&quot;maxwi&quot;</span><br><span class="line">echo &quot;Hello $&#123;var&#125;.com&quot;</span><br></pre></td></tr></table></figure>

<h3 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h3><p><strong>在shell的语句中间执行命令只需要将命令放在”`”（ESC下面那个键）之间即可，当然任何命令都可以在命令前面没有内容时直接执行：</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">cd $HOME;ls #进入个人目录并执行ls</span><br><span class="line">var=`ls` #ls结果以空格分隔的字符串返回给var</span><br><span class="line">for file in $var  # for...in循环输出以空格分隔的字符串内容</span><br><span class="line">do</span><br><span class="line">  echo $file</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<p>多个命令通过分号（;）并入一行执行</p>
<p><strong>只读变量</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var=8</span><br><span class="line">readonly var</span><br><span class="line">var=9 #出错</span><br></pre></td></tr></table></figure>

<p><strong>删除变量</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unset var</span><br></pre></td></tr></table></figure>

<p><strong>拼接字符直接将其放在一起即可</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a=I</span><br><span class="line">b=&#x27;love&#x27;</span><br><span class="line">c=&quot;China&quot;</span><br><span class="line">d=&quot;$a $b $c&quot;</span><br><span class="line">echo $d</span><br></pre></td></tr></table></figure>
<p>输出：<code>I love China</code></p>
<p><strong>获取字符串长</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo $&#123;#var&#125;</span><br></pre></td></tr></table></figure>

<p><strong>字符串切片</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo $&#123;var:2:5&#125;</span><br></pre></td></tr></table></figure>
<p>注意字符串中字符是从0计数，上述切片内容是从第3个字符到第6个字符，包含位置2和5位置上的字符</p>
<h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><p>数组下标从0开始，使用小括号（<code>()</code>）定义数组，元素以空格或回车分隔：<code>arr=(val1, val2, val3)</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$</span><span class="language-bash">&#123;arr[index]&#125; <span class="comment"># 使用下标获取相应元素</span></span></span><br><span class="line">echo $&#123;arr[@]&#125; or echo $&#123;arr[*]&#125; # 获取数组中的所有元素</span><br><span class="line">echo $&#123;#arr[@]&#125; # 获取数组长度，当然里面可以是  *</span><br></pre></td></tr></table></figure>


<p><code>for...in</code>循环可以用于遍历数组中的元素</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var=(1 2 3 I &#x27;love&#x27; &quot;China&quot;)</span><br><span class="line">for v in $&#123;var[@]&#125;</span><br><span class="line">do</span><br><span class="line">  str=&quot;$str $v&quot;</span><br><span class="line">done</span><br><span class="line">echo $str</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 I love China</span><br></pre></td></tr></table></figure>

<h2 id="命令行参数"><a href="#命令行参数" class="headerlink" title="命令行参数"></a>命令行参数</h2><p>通过<code>$n</code>来访问传递给shell脚本的命令行参数，<code>$0</code>为脚本本身，下面为几个特殊的参数：</p>
<table>
<thead>
<tr>
<th>参数</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>$#</td>
<td>传递到脚本的参数个数</td>
</tr>
<tr>
<td>$*</td>
<td>以一个单字符串显示所有向脚本传递的参数。如”$*”用「”」括起来的情况、以”$1 $2 … $n”的形式输出所有参数。即无法用for…in遍历每一个元素</td>
</tr>
<tr>
<td>$$</td>
<td>脚本运行的当前进程ID号</td>
</tr>
<tr>
<td>$!</td>
<td>后台运行的最后一个进程的ID号</td>
</tr>
<tr>
<td>$@</td>
<td>与$*相同，但是使用时加引号，并在引号中返回每个参数。 如”$@”用「”」括起来的情况、以”$1” “$2” … “$n” 的形式输出所有参数。</td>
</tr>
<tr>
<td>$-</td>
<td>显示Shell使用的当前选项，与set命令功能相同。</td>
</tr>
<tr>
<td>$?</td>
<td>显示最后命令的退出状态。0表示没有错误，其他任何值表明有错误。”</td>
</tr>
</tbody></table>
<h2 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h2><h3 id="数字关系运算符"><a href="#数字关系运算符" class="headerlink" title="数字关系运算符"></a>数字关系运算符</h3><p>该运算符只支持数字：</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>说明</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>-eq</td>
<td>检测两个数是否相等，相等返回 true。</td>
<td>[ $a -eq $b ] 返回 false。</td>
</tr>
<tr>
<td>-ne</td>
<td>检测两个数是否相等，不相等返回 true。</td>
<td>[ $a -ne $b ] 返回 true。</td>
</tr>
<tr>
<td>-gt</td>
<td>检测左边的数是否大于右边的，如果是，则返回 true。</td>
<td>[ $a -gt $b ] 返回 false。</td>
</tr>
<tr>
<td>-lt</td>
<td>检测左边的数是否小于右边的，如果是，则返回 true。</td>
<td>[ $a -lt $b ] 返回 true。</td>
</tr>
<tr>
<td>-ge</td>
<td>检测左边的数是否大于等于右边的，如果是，则返回 true。</td>
<td>[ $a -ge $b ] 返回 false。</td>
</tr>
<tr>
<td>-le</td>
<td>检测左边的数是否小于等于右边的，如果是，则返回 true。</td>
<td>[ $a -le $b ] 返回 true。</td>
</tr>
</tbody></table>
<p>也可以使用<code>==</code>和<code>!=</code></p>
<p>注意使用时必须使用方括号括住<br>eg:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">a=1</span><br><span class="line">b=2</span><br><span class="line">if [ $a -eq $b ]</span><br><span class="line">then</span><br><span class="line">   echo &quot;$a -eq $b : a 等于 b&quot;</span><br><span class="line">else</span><br><span class="line">   echo &quot;$a -eq $b: a 不等于 b&quot;</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>

<h3 id="布尔运算符"><a href="#布尔运算符" class="headerlink" title="布尔运算符"></a>布尔运算符</h3><ul>
<li><code>!</code> 取非，如[!false]返回true</li>
<li><code>-o</code> 或运算</li>
<li><code>-a</code> 与运算</li>
</ul>
<h3 id="逻辑运行符"><a href="#逻辑运行符" class="headerlink" title="逻辑运行符"></a>逻辑运行符</h3><ul>
<li><code>&amp;&amp;</code> 逻辑与</li>
<li><code>||</code> 逻辑或</li>
</ul>
<h3 id="字符串比较运算符"><a href="#字符串比较运算符" class="headerlink" title="字符串比较运算符"></a>字符串比较运算符</h3><table>
<thead>
<tr>
<th>运算符</th>
<th>说明</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>=</td>
<td>检测两个字符串是否相等，相等返回 true。</td>
<td>[ $a = $b ] 返回 false。</td>
</tr>
<tr>
<td>!=</td>
<td>检测两个字符串是否相等，不相等返回 true。</td>
<td>[ $a != $b ] 返回 true。</td>
</tr>
<tr>
<td>-z</td>
<td>检测字符串长度是否为0，为0返回 true。</td>
<td>[ -z $a ] 返回 false。</td>
</tr>
<tr>
<td>-n</td>
<td>检测字符串长度是否为0，不为0返回 true。</td>
<td>[ -n $a ] 返回 true。</td>
</tr>
<tr>
<td>str</td>
<td>检测字符串是否为空，不为空返回 true。</td>
<td>[ $a ] 返回 true。</td>
</tr>
</tbody></table>
<h3 id="文件测试运算符"><a href="#文件测试运算符" class="headerlink" title="文件测试运算符"></a>文件测试运算符</h3><p>常用的几个</p>
<ul>
<li><code>[-f file]</code> 检测文件是否为普通文件，既不能是目录也不能是设备</li>
<li><code>[-d file]</code> 检测是否为目录</li>
<li><code>[-s file]</code> 检测文件是否为空</li>
<li><code>[-e file]</code> 检测文件是否存在</li>
<li><code>[-r file]</code> 检测文件是否可读</li>
<li><code>[-w file]</code> 检测文件是否可写</li>
<li><code>[-x file]</code> 检测文件是否可执行</li>
</ul>
<h3 id="通过expr实现的算术运行符"><a href="#通过expr实现的算术运行符" class="headerlink" title="通过expr实现的算术运行符"></a>通过expr实现的算术运行符</h3><p>expr命令可以执行算术表达式并返回结果<br>如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">a=1</span><br><span class="line">b=2</span><br><span class="line"></span><br><span class="line">val=`expr $a + $b`</span><br><span class="line"></span><br><span class="line">val=`expr 9 \* $b`</span><br></pre></td></tr></table></figure>

<p>注意乘运算需要有转义符，表达示之间必须用空格隔开</p>
<h2 id="输入、输出控制"><a href="#输入、输出控制" class="headerlink" title="输入、输出控制"></a>输入、输出控制</h2><h3 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h3><p><code>echo</code>默认输出会换行，可以使用<code>\c</code>转义实现不换行<br>可以通过<code>echo</code>直接回显命令执行结果：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo `date`</span><br></pre></td></tr></table></figure>
<p>可以使用linux下类型C语言中printf的格式控制输出命令，详见<code>man printf</code></p>
<h3 id="输入"><a href="#输入" class="headerlink" title="输入"></a>输入</h3><p>通过<code>read</code>读取输入：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &#x27;Input number&#x27;</span><br><span class="line">read a b c d</span><br></pre></td></tr></table></figure>
<p>如果输入为<code>1 2 3 4 5 6</code>，则a,b,c分别为1,2,3，但d是<code>4 5 6</code></p>
<h3 id="文件重定向"><a href="#文件重定向" class="headerlink" title="文件重定向"></a>文件重定向</h3><p>shell的重定向功能非常强大，重定向命令列表如下：</p>
<p>| 命令 | 说明 |<br>|—|—|—| |<br>| <code>command &gt; file</code> |将标准输出重定向到file。 |<br>| <code>command &lt; file</code> |将标准输入重定向到file。 |<br>| <code>command &gt;&gt; file</code> | 将输出以追加的方式重定向到 file。 |<br>| <code>n&gt;file</code> | 将文件描述符为n的文件重定向到 file。<strong>注意不能有空格，下同</strong> |<br>| <code>n&gt;&gt;file</code> | 将文件描述符为 n 的文件以追加的方式重定向到 file。 |<br>| <code>n&gt;&amp;m</code> | 将输出流n合并到m。可以理解为使用&amp;n来引用名为n的文件描述符，即将文件描述符n重定向到文件描述符m |<br>| <code>n&lt;&amp;m</code> | 将输入流m合并到n。 |<br>| <code>&lt;&lt; tag</code> | 将开始标记 tag 和结束标记 tag 之间的内容作为输入。</p>
<p><strong>文件描述符0通常是标准输入（stdin），1是标准输出（stdout），2是标准错误输出（stderr），<code>/dev/null为黑洞文件，所有写入到它的内容都会被丢弃</code>，如果不指定数字，则默认为输出为1，输入为0</strong><br>查看系统文件描述符路径：<code>ls /proc/self/fd/ -l</code><br>几个特殊的用法：<br><strong><code>n&gt;&amp;-</code></strong><br><code>n&gt;&amp;-</code>表示关闭输出文件描述符n，如<code>1&gt;&amp;-</code>或<code>&gt;&amp;-</code>表示关闭标准输出；类似的<code>n&lt;&amp;-</code>表示关闭输入文件描述符n，如<code>0&lt;&amp;-</code>或<code>&lt;&amp;-</code>表示关闭标准输出。</p>
<p><strong><code>|&amp;</code></strong><br><code>|&amp;</code>等价于<code>2&gt;&amp;1</code>，即将标准错误合并到标准输出并作为管道的标准输入，输出结果中标准错误将在标准输出的前面，用法为cmd1 |&amp; cmd2，cmd2将以cmd1的输出和错误输出作为输入。</p>
<p><strong><code>&amp;&gt;/dev/null</code></strong><br><code>&amp;&gt;/dev/null</code>等价于<code>&gt;/dev/null 2&gt;&amp;1</code>，即将标准输出和错误输出重定向到null，什么也不会输出</p>
<p><strong><code>&gt;/dev/null</code></strong><br>等价于<code>1&gt;/dev/null</code>，即将标准输出重定向到null，只会输出标准错误</p>
<p>举例：<br>1.<code>cat ss.py &gt; cat_test.txt</code>，获取文件ss.py中的内容，并将其重定向到文件cat_test.txt，由于没有指定文件描述符，所以默认为将cat输出到标准输出中的内容重定向到文件。其等价于<code>cat ss.py 1&gt;cat_test.txt</code>。<br>2.<code>ls tes ss.py 1&gt;out.txt 2&gt;err.txt</code> 表示将标准输出输出到文件out.txt，标准错误输出到文件err.txt<br>3.<code>ls tes ss.py &gt; oe.txt 2&gt;&amp;1</code> 表示将标准错误输出重定向到标准输出，并将标准输出重定向到文件oe.txt。<br>4.<code>ls tes ss.py 1&gt;&amp;- 2&gt;&amp;-</code> 关闭标准输出和错误输出，效果等价于以下几个命令<code>ls tes ss.py 1&gt;/dev/null 2&gt;/dev/null</code>，<code>ls tes ss.py 1&gt;/dev/null 2&gt;&amp;1</code>，<code>ls tes ss.py &gt;/dev/null 2&gt;&amp;1</code>， <code>ls tes ss.py &amp;&gt;/dev/null</code><br>5.<code>cat &gt; t.txt</code> 将标准输入重定向到t.txt，输入内容之后，通过ctrl+d发送文件结束符停止输入。<br>6.<code>cat &gt; t.txt &lt; ss.py</code> 从ss.py读取数据，并重定向到t.txt</p>
<p><strong>Here Document是shell用于将delimiter之间的内容重定向到命令的特殊重定向：</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">command &lt;&lt; delimiter</span><br><span class="line">    document</span><br><span class="line">delimiter</span><br></pre></td></tr></table></figure>
<p>注意，第二个<code>delimiter</code>必须顶格写，这样其之间的document内容将都会传递给command</p>
<p><strong>修改当前shell session下的所有命令重定向</strong><br><code>exec 1&gt;out.txt</code>，执行之后会将当前shell窗口下执行的所有命令的标准输出内容重定向到文件out.txt，而在命令行窗口中不会输出任何内容。可以通过关闭当前shell终端并重新开一个来解决。也可以通过提前将标准输出绑定到一个新的文件描述符，重定向到文件之后，再将其绑回来的方式解决。<br>如：<br><code>exec 6&gt;&amp;1</code>，将6绑定到标准输出1，可以通过<code>ls /proc/self/fd/ -l</code>查看。<br><code>exec 1&gt;out.txt</code>，进行一些操作，发现没有输出，实际上结果都在out.txt中<br><code>exec 1&gt;&amp;6</code>，将1绑定到文件描述符6，其实就是最初是标准输出<br><code>exec 6&gt;&amp;-</code> 现关闭文件描述符6即可</p>
<h2 id="控制流"><a href="#控制流" class="headerlink" title="控制流"></a>控制流</h2><h3 id="if"><a href="#if" class="headerlink" title="if"></a>if</h3><p>一个完整的if控制流程：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">if condition1</span><br><span class="line">then</span><br><span class="line">    command1</span><br><span class="line">elif condition2</span><br><span class="line">then</span><br><span class="line">    command2</span><br><span class="line">else</span><br><span class="line">    commandN</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>

<h3 id="for-in"><a href="#for-in" class="headerlink" title="for..in"></a>for..in</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">for var in item1 item2 ... itemN</span><br><span class="line">do</span><br><span class="line">    command1</span><br><span class="line">    command2</span><br><span class="line">    ...</span><br><span class="line">    commandN</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<p>列表也可以是文件名</p>
<h3 id="for"><a href="#for" class="headerlink" title="for()"></a>for()</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>((i=1;i&lt;=10;i++));<span class="keyword">do</span> <span class="built_in">echo</span> $(<span class="built_in">expr</span> <span class="variable">$i</span> \* 4);<span class="keyword">done</span></span><br></pre></td></tr></table></figure>

<h3 id="while"><a href="#while" class="headerlink" title="while"></a>while</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">while condition</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<p>三种死循环：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">while :</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">for (( ; ; ))</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="until"><a href="#until" class="headerlink" title="until"></a>until</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">until condition</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<p>条件为真是停止，类似do..while，循环体至少执行一次</p>
<h3 id="case"><a href="#case" class="headerlink" title="case"></a>case</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">case 值 in</span><br><span class="line">模式1)</span><br><span class="line">    command1</span><br><span class="line">    command2</span><br><span class="line">    ...</span><br><span class="line">    commandN</span><br><span class="line">    ;;</span><br><span class="line">模式2）</span><br><span class="line">    command1</span><br><span class="line">    command2</span><br><span class="line">    ...</span><br><span class="line">    commandN</span><br><span class="line">    ;;</span><br><span class="line">esac</span><br></pre></td></tr></table></figure>
<p>每一个格式后面为右括号结束，模式匹配后一直执行，直到<code>;;</code>（与break功能一样）然后执行<code>*)</code>后面的命令，不再匹配 其他选项。</p>
<p><strong>支持break、continue</strong></p>
<h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><p>函数的定义方式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">func() &#123;</span><br><span class="line">  command</span><br><span class="line">  return val</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>调用直接函数名后跟参数列表即可：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">func arg1 arg2</span><br></pre></td></tr></table></figure>
<p>参数性质与上面说的命令行参数一样，也有那几个特殊参数<br>注意参数超过10个时必须使用<code>&#123;&#125;</code>引用，如<code>$&#123;10&#125;</code><br>函数返回值通过$?</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">add() &#123;</span><br><span class="line">  return $(($1+$2))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>调用：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">add 3 5</span><br><span class="line">echo $?</span><br></pre></td></tr></table></figure>

<h2 id="Shell多脚本内容共享"><a href="#Shell多脚本内容共享" class="headerlink" title="Shell多脚本内容共享"></a>Shell多脚本内容共享</h2><p>当有两个shell脚本文件时，可以通过<code>. filename</code>或<code>source filename</code>来引用另一个文件中的代码，其实就是同一个shell session之中信息是共享的。</p>
<h2 id="其他相关内容"><a href="#其他相关内容" class="headerlink" title="其他相关内容"></a>其他相关内容</h2><h3 id="eval"><a href="#eval" class="headerlink" title="eval"></a>eval</h3><p>脚本中需要使用到的命令<br>用法为<code>eval cmd args</code><br>该命令后面跟的所有内容都做为其参数，它会对后面的内容（即cmd args）进行两次扫描，第一次扫描将其中的变量替换为实际值，所以如果args中含有需要在bash中执行的以$开头的内容，应该加上转换符（<code>\</code>），以避免被替换，例如如果cmd为awk，而awk是通过$来进行参数过滤的。第二次扫描时将eval后面的所有内容当作同一个命令组合来执行，相当于你直接在命令行中执行该命令。例如脚本如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a=5</span><br><span class="line">cmd=&quot;echo $a | cat &gt; tcat.txt&quot;</span><br><span class="line">eval $cmd</span><br></pre></td></tr></table></figure>
<p>如果对cmd的执行不通过eval调用，而是直接<code>$cmd</code>，输出结果为<code>5 | cat &gt; tcat.txt</code>。这显然不是我们想要的，shell将echo后面的所有内容当成了echo参数来执行。这种情况就需要使用eval。</p>
<h3 id="exec"><a href="#exec" class="headerlink" title="exec"></a>exec</h3><p>与source功能有点相反，source命令会在当前shell进程的上下文件环境中执行各命令。而exec虽然也不会创建新的进程，但会清除当前shell进程的上下文件内容，并执行相应命令。当然它也可以用来对文件描述符进行操作，上文已经提及。</p>
<h3 id="declare-typeset"><a href="#declare-typeset" class="headerlink" title="declare(typeset)"></a>declare(typeset)</h3><p>内建命令declare与typeset功能类似，用于为变量指定类型，因为默认所有变量都会被当成字符串，当然数组除外。<br>用法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">declare [-aAfFgilnrtux] [-p] [name[=value] ...]</span><br><span class="line">typeset [-aAfFgilnrtux] [-p] [name[=value] ...]</span><br></pre></td></tr></table></figure>
<p>例如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">a=1</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">b=2</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">echo</span> <span class="variable">$a</span>+<span class="variable">$b</span></span></span><br><span class="line">1+2</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">declare</span> -i a=1</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">declare</span> -i b=2</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">declare</span> -i c</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">c=<span class="variable">$a</span>+<span class="variable">$b</span></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">echo</span> <span class="variable">$c</span></span></span><br><span class="line">3</span><br></pre></td></tr></table></figure>


<h3 id="local"><a href="#local" class="headerlink" title="local"></a>local</h3><p>用于在shell函数内部声明该变量为局部变量，只对当前函数或其子进程有效。用法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">local [option] [name[=value] ...]</span><br></pre></td></tr></table></figure>
<p>option为declare可以接受的选项。</p>
<p><strong>一不小心这里就会有一个很大的坑，因为默认情况下shell中的变量作用域是全局的，所以你一个for用了i，然后又在另一个函数调用中用了i，那这个i就会被前面的i覆盖，所以尽量在函数中有类似情况的地方用local</strong></p>
<h2 id="用例"><a href="#用例" class="headerlink" title="用例"></a>用例</h2><h3 id="输出系统中的所有磁盘"><a href="#输出系统中的所有磁盘" class="headerlink" title="输出系统中的所有磁盘"></a>输出系统中的所有磁盘</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">disks=`fdisk -l | grep &#x27;Disk /dev/&#x27; | grep -oP &#x27;/dev/.&#123;3&#125;&#x27;`</span><br><span class="line">for disk in $disks</span><br><span class="line">do</span><br><span class="line">    echo &#x27;**&#x27;</span><br><span class="line">    echo $disk</span><br><span class="line">    echo &#x27;**&#x27;</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p>1.<a target="_blank" rel="noopener" href="https://www.shellscript.sh/">https://www.shellscript.sh/</a><br>2.<a target="_blank" rel="noopener" href="http://ryanstutorials.net/bash-scripting-tutorial/">http://ryanstutorials.net/bash-scripting-tutorial/</a><br>3.<a target="_blank" rel="noopener" href="http://www.runoob.com/linux/">http://www.runoob.com/linux/</a></p>

    </div>

    
    
    
        

  <div class="followme">
    <p>Welcome to my other publishing channels</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Linux/" rel="tag"># Linux</a>
              <a href="/tags/shell/" rel="tag"># shell</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2017/04/20/java-jni-cpp-passing-arguments/" rel="prev" title="Java通过JNI调用C/C++动态链接库之参数传递及结果返回">
      <i class="fa fa-chevron-left"></i> Java通过JNI调用C/C++动态链接库之参数传递及结果返回
    </a></div>
      <div class="post-nav-item">
    <a href="/2017/08/18/git-submodule/" rel="next" title="Git submodule使用方法">
      Git submodule使用方法 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B3%E4%BA%8E"><span class="nav-number">1.</span> <span class="nav-text">关于</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%98%E9%87%8F"><span class="nav-number">2.</span> <span class="nav-text">变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">2.1.</span> <span class="nav-text">字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E7%BB%84"><span class="nav-number">2.2.</span> <span class="nav-text">数组</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0"><span class="nav-number">3.</span> <span class="nav-text">命令行参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">4.</span> <span class="nav-text">运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E5%AD%97%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">4.1.</span> <span class="nav-text">数字关系运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%83%E5%B0%94%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">4.2.</span> <span class="nav-text">布尔运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E8%A1%8C%E7%AC%A6"><span class="nav-number">4.3.</span> <span class="nav-text">逻辑运行符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">4.4.</span> <span class="nav-text">字符串比较运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">4.5.</span> <span class="nav-text">文件测试运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%9A%E8%BF%87expr%E5%AE%9E%E7%8E%B0%E7%9A%84%E7%AE%97%E6%9C%AF%E8%BF%90%E8%A1%8C%E7%AC%A6"><span class="nav-number">4.6.</span> <span class="nav-text">通过expr实现的算术运行符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BE%93%E5%85%A5%E3%80%81%E8%BE%93%E5%87%BA%E6%8E%A7%E5%88%B6"><span class="nav-number">5.</span> <span class="nav-text">输入、输出控制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%93%E5%87%BA"><span class="nav-number">5.1.</span> <span class="nav-text">输出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%93%E5%85%A5"><span class="nav-number">5.2.</span> <span class="nav-text">输入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E9%87%8D%E5%AE%9A%E5%90%91"><span class="nav-number">5.3.</span> <span class="nav-text">文件重定向</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A7%E5%88%B6%E6%B5%81"><span class="nav-number">6.</span> <span class="nav-text">控制流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#if"><span class="nav-number">6.1.</span> <span class="nav-text">if</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#for-in"><span class="nav-number">6.2.</span> <span class="nav-text">for..in</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#for"><span class="nav-number">6.3.</span> <span class="nav-text">for()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#while"><span class="nav-number">6.4.</span> <span class="nav-text">while</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#until"><span class="nav-number">6.5.</span> <span class="nav-text">until</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#case"><span class="nav-number">6.6.</span> <span class="nav-text">case</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%BD%E6%95%B0"><span class="nav-number">7.</span> <span class="nav-text">函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Shell%E5%A4%9A%E8%84%9A%E6%9C%AC%E5%86%85%E5%AE%B9%E5%85%B1%E4%BA%AB"><span class="nav-number">8.</span> <span class="nav-text">Shell多脚本内容共享</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E7%9B%B8%E5%85%B3%E5%86%85%E5%AE%B9"><span class="nav-number">9.</span> <span class="nav-text">其他相关内容</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#eval"><span class="nav-number">9.1.</span> <span class="nav-text">eval</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#exec"><span class="nav-number">9.2.</span> <span class="nav-text">exec</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#declare-typeset"><span class="nav-number">9.3.</span> <span class="nav-text">declare(typeset)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#local"><span class="nav-number">9.4.</span> <span class="nav-text">local</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%94%A8%E4%BE%8B"><span class="nav-number">10.</span> <span class="nav-text">用例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%93%E5%87%BA%E7%B3%BB%E7%BB%9F%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%A3%81%E7%9B%98"><span class="nav-number">10.1.</span> <span class="nav-text">输出系统中的所有磁盘</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">11.</span> <span class="nav-text">参考</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="blueyi"
      src="/images/default_avatar.jpg">
  <p class="site-author-name" itemprop="name">blueyi</p>
  <div class="site-description" itemprop="description">心怀善意，虛怀若谷！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">104</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">68</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/blueyi" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;blueyi" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://maxwi.com/" title="http:&#x2F;&#x2F;maxwi.com" rel="noopener" target="_blank">Maxwi</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2014 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">blueyi</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="Symbols count total">750k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">11:22</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '0f8243eb2c8b2207980f',
      clientSecret: 'd159633a33519d3b7a48b0ca729032f7d1f38a41',
      repo        : 'notes',
      owner       : 'blueyi',
      admin       : ['blueyi'],
      id          : '65b890eea0cb84e52e44301c454f7d84',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

    </div>
</body>
</html>
